﻿using System;
using System.Collections.Generic;
using System.Linq;
using MvcDesign.Core.CoreDataModel;

namespace MvcDesign.Core.DataAccess
{
    public static class DataInitializer
    {
        private static readonly List<IDataInitializer> Initializers = new List<IDataInitializer>();

        public static void RegisterInitializer(IDataInitializer initializer)
        {
            Initializers.Add(initializer);
        }

        public static void Initialize(DataContext context)
        {
            if (context.GetCollection<User>().Any())
            {
                return;
            }

            var testUser = new User
            {
                Name = "Test User",
                Email = "user@email",
                Created = DateTime.UtcNow,
                Comment = "Comment",
                DisplayName = "Test User",
                IsApproved = true,
                IsLockedOut = false,
                LastActivity = DateTime.UtcNow,
                LastLockout = null,
                LastLogin = DateTime.UtcNow,
                LastPasswordChange = null,
                Password = "aaa",
                Updated = DateTime.UtcNow,
            };

            context.GetCollection<User>().Add(testUser);

            var application = new Application
            {
                Description = "Forum", 
                Name = "Forum", 
                Link = "Forum",
                Enabled = true,
            };

            context.GetCollection<Application>().Add(application);

            application = new Application
            {
                Description = "Blog",
                Name = "Blog",
                Link = "Blog",
                Enabled = true,
            };

            context.GetCollection<Application>().Add(application);

            context.GetCollection<ConfigurationItem>().Add(new ConfigurationItem {Id = "Name", Value = "Test Forum"});
            context.GetCollection<ConfigurationItem>().Add(new ConfigurationItem {Id = "Description", Value = "Test Forum Description"});

            foreach (var initializer in Initializers)
            {
                initializer.Initialize(context);
            }

            context.SaveChanges();
        }
    }
}
